(function () {

    fetch('http://chst.vip:1234/api/getcategorytitle').then((res) => {
        // console.log(typeof res.json())
        return res.json()
    }).then(res => {
        // console.log(res.result)
        let str = ''
        res.result.forEach(item => {
            str += `
            <li titleid=${item.titleId}>
            <div class="items_title">
                <span>${item.title}</span>
                <span class="iconfont icon-xiala"></span>
            </div>
            <div class="items_bottom">
            </div>
        </li>

            `
        })
        $('.items').html(str)

    }).then(() => {
        console.log($('.items li'))
        let arr = $('.items li')
        // console.log(typeof arr)
        // console.log(arr['0'])
        //    console.log(arr.length)
        for (let i = 0; i < arr.length; i++) {
            // console.log(arr[i])
            // console.log($(arr[i]).attr('titleid'))
            fetch(`http://chst.vip:1234/api/getcategory?titleid=${$(arr[i]).attr('titleid')}`).then(res => {
                return res.json()
            }).then(res => {
                let str = ''
                // console.log(res.result)
                res.result.forEach(item => {
                    str += `
                    <span class="ck" categoryId=${item.categoryId}>${item.category}</span>
                    `
                })
                $(arr[i]).find('.items_bottom').html(str)
            })
        }
        $('.items_bottom').slideUp()
        let flag = true

        $('.items_title').click(function () {
            if (flag) {
                $(this).next().slideDown(0).parent().siblings().find('.items_bottom').slideUp(0)
                flag = false
            } else {
                $('.items_bottom').slideUp(0)
                flag = true
            }


        })




    })

    //点击跳转
    $('.items').click(function (e) {
        // console.log(e.target.nodeName)
        if (e.target.nodeName == 'SPAN') {
            let id = e.target.getAttribute('categoryId')
            // console.log(id)
            if(id == null) return
            location.href = `../pages/CsParticulars.html?categoryId=${id}`
        }
    })


})()